package com.example.composedemo

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.composedemo.ui.theme.ComposeDemoTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    GetPage("Android")
                }
            }
        }
    }
}

@Composable
fun GetPage(name: String) {
    val context = LocalContext.current
    Column() {
        Scaffold(topBar = {
            TopAppBar(title = { Text(text = "标题") })
        },
            content = {
                Column() {
                    for (i in 0..10) {
                        getRow("数据$i")
                    }
                }
            }
        )
    }
}

@Composable
fun getRow(text: String) {
    val context = LocalContext.current
    Row(
        modifier = Modifier.padding(4.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(text = text)
        Button(onClick = {
            Toast.makeText(context, "点击了 $text", Toast.LENGTH_LONG).show()
        }, modifier = Modifier.padding(4.dp))
        {
            Text(text = "点击")
        }
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ComposeDemoTheme {
        GetPage("Android")
    }
}